import requests
from diploma_project import settings
class OpenidUtils():
    def __init__(self, jscode):
        self.url = "https://api.weixin.qq.com/sns/jscode2session"
        self.appid = settings.APPID
        self.secret = settings.SECRET
        self.jscode = jscode  # 前端传回的动态jscode

    def get_openid(self):
        code_url = self.url + "?appid=" + self.appid + "&secret=" + self.secret + "&js_code=" + self.jscode + "&grant_type=authorization_code"
        # r = requests.get(url)
        # openid = r.json()['openid']
        # return openid
        response = requests.get(code_url)
        json_response = response.json()  # 把它变成json的字典
        if json_response.get("session_key"):
            return json_response
        else:
            return False

# open_obj=OpenidUtils('013kXx0w34fGnW2Dd41w34Yr1V3kXx0T')
# data=open_obj.get_openid()
# print(data["openid"])
# print(type(data))